home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / rss / getoptva.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  3.6 KB  |  127 lines

  1. /*                              G E T O P T V A . C
  2.  
  3.     % 1 name
  4. \functoc {getoptval}
  5.     % 2 declaration
  6. {char *\fname (\params\ )}
  7.     % 3 arguments
  8. {
  9.     {int *}{p_argc}{pointer to argument counter}
  10.     {char **}{argv}{pointer to argument strings}
  11. }
  12.     % 4 return value
  13. {A pointer to the string following an optioncharacter, or NULL if no such
  14. string is found.}
  15.     % 5 functions used
  16. {}
  17.     % 6 see also
  18. {getopt, setoptchars, getoptindex}
  19.     % 7 source file
  20. {getoptva.c}
  21.     % 8 description
  22. {The function will return a pointer to the string immediately following an
  23. option string if such a string exists. The following cases are recognized:
  24. \begin{itemize}
  25.     \item An option and its extra information forms one string, as in, e.g.,
  26.         {\tt -s60}. The option is {\tt -s}, a pointer to the string {\tt 60}
  27.         is returned.
  28.     \item An option and its extra information form two strings, as in, e.g.,
  29.         {\tt -s 60}. The option is {\tt -s}, a pointer to the string {\tt 60}
  30.         is returned. As the string ``60'' is considered part of the option, it
  31.         is removed from the argument vector, and the corresponding argument
  32.         count is reduced.
  33.     \item An option is not followed by a string, but immediately by another
  34.         option, as in {\tt -s -q}. The option is {\tt -s}, and function
  35.         \Function{\fname} returns NULL.
  36.     \item An option was the last entry on the command line, and
  37.         is not followed by a string as in {\tt -s}. The option is {\tt -s},
  38.         and function \Function{\fname} returns NULL.
  39. \end{itemize}
  40.  
  41. Function \Function{\fname} should be called only if function \Function{getopt}
  42. dit not return \Define{-1}. All other cases produce unpredicable results.
  43. }
  44.     % 9 example
  45. {}
  46. \footnotesize
  47. \begin{verbatim}
  48.  
  49. #include <icce.h>
  50.  
  51. void main(int argc, char **argv)
  52. {
  53.     int
  54.         c;
  55.  
  56.     while ((c = getopt(&argc, argv)) != -1)
  57.     {
  58.         if (val = getoptval(&argc, argv))
  59.             printf("Option value is: %s\n", val);
  60.         else
  61.             puts("No option value");
  62.     }
  63.  
  64.     rest_of_program();
  65. }
  66.  
  67. \end{verbatim}
  68. \normalsize
  69. */
  70.  
  71. #ifndef MSDOS
  72.  
  73. #include <stdio.h>
  74. #include <string.h>
  75. #include "icrss.h"
  76.  
  77. extern char
  78.     *near icce_beyond_optchar,
  79.     *near icce_optchar,
  80.     *near icce_optval;
  81. extern int
  82.     near icce_next_option;
  83.  
  84. static char
  85.     *near optval;
  86.  
  87. char *ic_getoptval(int *p_argc, char **argv)
  88. {
  89.     register int
  90.         argc,
  91.         index;
  92.  
  93.     if
  94.     (
  95.         icce_beyond_optchar                 /* option character found */
  96.         &&                                  /* and */
  97.         *icce_optval                        /* something trailing */
  98.     )
  99.     {
  100.         icce_beyond_optchar = NULL;
  101.         return (icce_optval);               /* return pointer to trailing info */
  102.     }
  103.  
  104.     icce_beyond_optchar = NULL;             /* option processed */
  105.     argc = *p_argc;                         /* get local argc value */
  106.  
  107.     if
  108.     (                                       /* next one is option ? */
  109.         strchr(icce_optchar, *argv[icce_next_option])
  110.         ||                                  /* or */
  111.         icce_next_option >= argc            /* no more entries */
  112.     )
  113.         return (NULL);                      /* then no value string */
  114.  
  115.     optval = argv[icce_next_option];        /* prepare return value */
  116.  
  117.                                             /* shift arguments */
  118.     for (index = icce_next_option + 1; index <= argc; index++)
  119.         argv[index - 1] = argv[index];
  120.  
  121.     (*p_argc)--;                            /* reduce external count */
  122.  
  123.     return (optval);                        /* return the option value. */
  124. }
  125.  
  126. #endif                        /* ! MSDOS */
  127.